/*
 * @filename FileUtils.java
 * @author barry
 * @version 0.0.1
 * @date 2019年12月18日
 */
package com.bnzj.core.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.springframework.util.ResourceUtils;

/**
 * @author barry
 * @date 2019-12-18
 */
public class FileUtils {

    /**
     * 获取资源的IO流
     * @param resourceLocation
     * @return
     */
    public static InputStream getInputStream(String resourceLocation) {
        try {
            URL url = ResourceUtils.getURL(resourceLocation);
            if(url.toString().startsWith(ResourceUtils.JAR_URL_PREFIX)) {
                return FileUtils.class.getClassLoader().getResourceAsStream(resourceLocation.replace("classpath:", ""));
            }else {
                return new FileInputStream(ResourceUtils.getFile(url.toURI()));
            }
        } catch (FileNotFoundException | URISyntaxException e) {
            e.printStackTrace();
            return null;
        }
    }
}
